package com.zx.mylame;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.zx.mylame.jni.JniUtils;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private EditText et_wav;
    private EditText et_mp3;
    private ProgressDialog pd;
    JniUtils mJniUtils;

    public void setConvertProgress(int progress) {
        pd.setProgress(progress);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mJniUtils = new JniUtils();
        mJniUtils.setmCallBack(new JniUtils.CallBack() {
            @Override
            public void setProgress(int progress) {
                setConvertProgress(progress);
            }
        });
        et_wav = (EditText) this.findViewById(R.id.editText1);
        et_mp3 = (EditText) this.findViewById(R.id.editText2);
        pd = new ProgressDialog(this);
        // Example of a call to a native method
//        TextView tv = (TextView) findViewById(R.id.sample_text);
//        tv.setText(stringFromJNI());
    }

    public void convert(View view) {
        final String mp3name = et_mp3.getText().toString().trim();
        final String wavname = et_wav.getText().toString().trim();
        File file = new File(wavname);
        int size = (int) file.length();
        System.out.println("文件大小 " + size);
        if ("".equals(mp3name) || "".equals(wavname)) {
            Toast.makeText(this, "路径不能为空", Toast.LENGTH_LONG).show();
            return;
        }
        //convertmp3(wavname,mp3name);
        pd.setMessage("转换中....");
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(size); // 设置进度条的最大值
        //pd.setCancelable(false);
        pd.show();
        new Thread() {

            @Override
            public void run() {
                mJniUtils.convertmp3(wavname, mp3name);
                pd.dismiss();
            }

        }.start();
    }

    public void getversion(View view) {
        String version = mJniUtils.getLameVersion();
        Toast.makeText(this, version, Toast.LENGTH_SHORT).show();
    }
}
